<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 二分法查找 答案
        function binarySearch(arr, target) {
            var left = 0;
            var right = arr.length - 1;
            while (left <= right) {
                var mid = Math.floor((left + right) / 2);
                if (arr[mid] === target) {
                    return mid; // 找到目标值，返回索引
                } else if (arr[mid] < target) {
                    left = mid + 1; // 目标值在右侧，缩小查找范围
                } else {
                    right = mid - 1; // 目标值在左侧，缩小查找范围 }
                }
                return -1; // 目标值不存在，返回 -1
            }
        }
    </script>
</body>

</html>